<template>
  <el-dialog
    v-el-drag-dialog
    title="检查设置"
    :visible.sync="isShow"
    width="500px"
    @closed="reset"
  >
    <el-form
      ref="dataFormRef"
      :rules="rules"
      :model="form"
      label-position="left"
      label-width="100px"
    >
      <el-form-item label="充装检查" prop="check">
        <el-switch v-model="form.check" active-text="已开启" />
      </el-form-item>
      <!-- <el-form-item label="用户类型:" prop="roles">
      </el-form-item> -->
      <el-form-item label="充装检查人:">
        <el-radio-group v-model="form.checkType">
          <el-radio :label="2">
            <div class="selectBox">
              <span>用户类型</span>
              <el-select
                v-model="form.roles"
                multiple
                collapse-tags
                placeholder="请选择用户类型"
                style="width: 100%;"
              >
                <el-option
                  v-for="(i, ind) in roleOption"
                  :key="ind"
                  :label="i.desc"
                  :value="i.value"
                />
              </el-select>
            </div>
          </el-radio>
          <el-radio :label="1">来源于充装检查排班 </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="reset"> 关闭 </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="save">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryMap, fillingcheckCheckSettingSave } from '@/api'
  export default {
    props: ['settingInfo'],
    data() {
      return {
        isShow: false,
        roleOption: [],
        form: {
          roles: [],
          check: true
        },
        rules: {
          roles: [{ required: true, message: '请选择检查人员', trigger: 'change' }]
        }
      }
    },
    methods: {
      show() {
        this.isShow = true
        if (this.settingInfo) {
          this.form.check = this.settingInfo.checked === 1
        }

        if (!this.roleOption.length) this.getItemType()
        else {
          const roles = (this.settingInfo && this.settingInfo.roles.split(',')) || []
          this.form.roles = roles.map((v) => {
            return Number(v)
          })
        }
      },
      reset() {
        this.$refs['dataFormRef'].resetFields()
        this.isShow = false
      },
      async getItemType() {
        const res = await queryMap({
          enumName: 'UserType'
        })
        this.roleOption = res.data
        const roles = (this.settingInfo && this.settingInfo.roles.split(',')) || []
        this.form.roles = roles.map((v) => {
          return Number(v)
        })
      },
      save() {
        this.$refs.dataFormRef.validate((valid) => {
          if (!valid) return
          this.submit()
        })
      },
      async submit() {
        const params = {
          checked: this.form.check ? 1 : 0,
          roles: this.form.roles.join(','),
          checkType: this.form.checkType
        }
        const { status } = await fillingcheckCheckSettingSave(params)

        if (status === 200) {
          this.$notify({
            title: '提示',
            message: '修改成功',
            type: 'success',
            duration: 2000
          })
          this.reset()
          this.$emit('refresh')
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
.selectBox {
  display: inline-block;
  margin-bottom: 20px;
}
.selectBox /deep/ .el-select {
  max-width: 160px;
}
</style>
